package com.shengxing.lljl.payment.Wechat.select.mpi;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.shengxing.lljl.payment.Wechat.Entity.*;
import com.shengxing.lljl.payment.Wechat.Handler.NativePayHandler;
import com.shengxing.lljl.payment.Wechat.select.QRCodeService;
import com.shengxing.lljl.payment.Wechat.Enum.PayChannelEnum;
import com.shengxing.lljl.payment.Wechat.Enum.TradingEnum;
import com.shengxing.lljl.payment.Wechat.Enum.TradingStateEnum;
import com.shengxing.lljl.payment.Wechat.Exception.SLException;
import com.shengxing.lljl.payment.Wechat.select.NativePayService;
import com.shengxing.lljl.payment.Wechat.select.TradingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;


/**
 * Native支付方式Face接口：商户生成二维码，用户扫描支付
 *
 * @author zzj
 * @version 1.0
 */
@Service
@Slf4j
public class NativePayServiceImpl implements NativePayService {


    @Resource
    private TradingService tradingService;//订单
    @Resource
    private BeforePayHandler beforePayHandler;
    @Resource
    private QRCodeService qrCodeService;

    @Override
    public String queryQrCodeUrl(Long tradingOrderNo) {
        TradingEntity trading = this.tradingService.findTradByTradingOrderNo(tradingOrderNo);
        if (ObjectUtil.equals(trading.getTradingState(), TradingStateEnum.YJS)) {
            //订单已完成，不返回二维码
            throw new SLException(String.valueOf(TradingEnum.TRADING_STATE_SUCCEED));
        }
        return trading.getQrCode();
    }

    @Override
    public TradingEntity createDownLineTrading(TradingEntity tradingEntity) {
        //交易前置处理：检测交易单参数
        Boolean flag = beforePayHandler.checkCreateTrading(tradingEntity);
        if (!flag) {
            throw new SLException(String.valueOf(TradingEnum.CONFIG_ERROR));
        }

        tradingEntity.setTradingType(TradingConstant.TRADING_TYPE_FK);
        tradingEntity.setEnableFlag(Constants.YES);


        try {
            //获取锁

                //交易前置处理：幂等性处理
                this.beforePayHandler.idempotentCreateTrading(tradingEntity);

                //调用不同的支付渠道进行处理
                PayChannelEnum payChannel = PayChannelEnum.valueOf(tradingEntity.getTradingChannel());
                NativePayHandler nativePayHandler = HandlerFactory.get(payChannel, NativePayHandler.class);
                nativePayHandler.createDownLineTrading(tradingEntity);

                //生成统一收款二维码
                String placeOrderMsg = tradingEntity.getPlaceOrderMsg();
                String qrCode = this.qrCodeService.generate(placeOrderMsg, payChannel);
                tradingEntity.setQrCode(qrCode);

                //新增或更新交易数据
                flag = this.tradingService.saveOrUpdate(tradingEntity);
                if (!flag) {
                    throw new SLException(String.valueOf(TradingEnum.SAVE_OR_UPDATE_FAIL));
                }

                return tradingEntity;


        } catch (SLException e) {
            throw e;
        } catch (Exception e) {
            log.error("统一收单线下交易预创建异常:{}", ExceptionUtil.stacktraceToString(e));
            throw new SLException(String.valueOf(TradingEnum.NATIVE_PAY_FAIL));
        }
    }
}
